package com.backend.controller.blog;

import com.backend.service.blog.ArticleService;
import com.common.constant.MessageConstant;
import com.common.result.Result;
import com.pojo.dto.AddArticleDTO;
import com.pojo.vo.ArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/blog/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    /**
     * 添加一篇文章
     */
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody AddArticleDTO addArticleDTO) {
        Integer result = articleService.addArticle(addArticleDTO);
        if (result == 0) {
            return Result.error(MessageConstant.ADD_ARTICLE_FAIL);
        }
        return Result.success(MessageConstant.ADD_ARTICLE_SUCCESS);
    }
    /**
     * 删除一篇文章
     */
    @DeleteMapping("/removeArticle/{id}")
    public Result removeArticle(@PathVariable("id") Long id) {
        Integer result = articleService.removeArticle(id);
        if (result == 0) {
            return Result.error("文章删除失败");
        }
        return Result.success("文章删除成功");
    }
    /**
     * 修改一篇文章
     */
    @PutMapping("/updateArticle/{id}")
    public Result updateArticle(@PathVariable("id") Long id, @RequestBody AddArticleDTO addArticleDTO) {
        Integer result = articleService.updateArticle(id, addArticleDTO);
        if (result == 0) {
            return Result.error("文章修改失败");
        }
        return Result.success("文章修改成功");
    }
    /**
     * 查看一篇文章
     */
    @GetMapping("/getArticle/{id}")
    public Result getArticleById(@PathVariable("id") Long id) {
        ArticleVO articleVO = articleService.getArticleById(id);
        if (articleVO == null) {
            return Result.error("未查到此文章");
        }
        return Result.success(articleVO);
    }
    /**
     * 通过userId查看他的所有文章
     */
    @GetMapping("/getArticles/{id}")
    public Result getArticlesByUserId(@PathVariable("id") Long id) {
        List<ArticleVO> articleVOS = articleService.getArticles(id);
        return Result.success(articleVOS);
    }
    /**
     * 给某篇文章点赞
     */
    @PutMapping("/likeArticle/{id}")
    public Result likeArticle(@PathVariable("id") Long id) {
        Integer result = articleService.likeArticle(id);
        if (result == 0) {
            return Result.error("点赞修改失败");
        }
        return Result.success("点赞修改成功");
    }
    /**
     * 给某篇文章设为置顶
     */
    @PutMapping("/setEssence/{id}")
    public Result setEssence(@PathVariable("id") Long id) {
        Integer result = articleService.setEssence(id);
        if (result == 0) {
            return Result.error("精华修改失败");
        }
        return Result.success("精华修改成功");
    }
    //TODO
    /**
     * 给某篇文章设置为精华
     */
    @PutMapping("/setTop/{id}")
    public Result setTop(@PathVariable("id") Long id) {
        Integer result = articleService.setTop(id);
        if (result == 0) {
            return Result.error("置顶修改失败");
        }
        return Result.success("置顶修改成功");
    }
}
